package com.bwei.like.mynewstop.activity;

import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.bwei.like.mynewstop.R;

import java.util.Timer;
import java.util.TimerTask;

import cn.smssdk.EventHandler;
import cn.smssdk.SMSSDK;

<<<<<<< 77331352d74e6afdfbbeaccca4370d33ffbea935
public class RegistActivity extends AppCompatActivity implements View.OnClickListener {
=======
public class RegistActivity extends BaseActivity implements View.OnClickListener {
>>>>>>> NewsTop

    private EditText et_text_phone;
    private EditText et_Code;
    private Button codeButton;
    private Button commitButton;
    private ImageView back_image;
    private EditText et_possword;
    private TimeCount timeCount = new TimeCount(60000,1000);
    private String number;


    EventHandler eh=new EventHandler(){

        @Override
        public void afterEvent(int event, int result, Object data) {

            if (result == SMSSDK.RESULT_COMPLETE) {
                //回调完成
                if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {
                    //提交验证码成功
                }else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE){
                    //获取验证码成功
                }else if (event ==SMSSDK.EVENT_GET_SUPPORTED_COUNTRIES){
                    //返回支持发送验证码的国家列表
                }
            }else{
                ((Throwable)data).printStackTrace();
            }
        }
    };
    @Override
<<<<<<< 77331352d74e6afdfbbeaccca4370d33ffbea935
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_regist);

=======
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       // supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_regist);
>>>>>>> NewsTop
        initView();
    }

    private void initView() {
        et_text_phone = (EditText) findViewById(R.id.et_text_phone);
        et_Code = (EditText) findViewById(R.id.et_Code);
        et_possword = (EditText)findViewById(R.id.et_password);
        back_image = (ImageView)findViewById(R.id.back_image);
        back_image.setOnClickListener(this);
        codeButton = (Button) findViewById(R.id.code_Button);
        codeButton.setOnClickListener(this);
        commitButton = (Button) findViewById(R.id.commit_Button);
        commitButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.back_image:
                finish();
                overridePendingTransition(R.anim.phone_in,R.anim.cell_regeist_out);
                break;

            case R.id.code_Button:
                timeCount.start();
                //获取输入的手机号
                number = et_text_phone.getText().toString().trim();
                SMSSDK.getVerificationCode("+86", number);
                break;

            case R.id.commit_Button:
                String phone_number = et_text_phone.getText().toString();
                String possword = et_possword.getText().toString().trim();
                String code = et_Code.getText().toString().trim();
                if (TextUtils.isEmpty(phone_number)){
                    Toast.makeText(RegistActivity.this, "手机号不能为空！", Toast.LENGTH_SHORT).show();
                }else if (TextUtils.isEmpty(possword)){
                    Toast.makeText(RegistActivity.this, "密码不能为空！", Toast.LENGTH_SHORT).show();
                }else if (phone_number.length()<11){
                    Toast.makeText(RegistActivity.this, "请输入正确的手机号！", Toast.LENGTH_SHORT).show();
                }else if (TextUtils.isEmpty(code)&&code.length()>4){
                    Toast.makeText(RegistActivity.this, "请输入正确的验证码！", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }

    class TimeCount extends CountDownTimer {
        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onFinish() {// 计时完毕
            codeButton.setText("获取验证码");
            codeButton.setClickable(true);
        }

        @Override
        public void onTick(long millisUntilFinished) {// 计时过程
            codeButton.setClickable(false);//防止重复点击
            codeButton.setText(millisUntilFinished / 1000 + "s");
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        SMSSDK.unregisterEventHandler(eh);
    }
}
